package com.glory.erp.auth.emp.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.glory.erp.auth.base.BaseDaoImpl;
import com.glory.erp.auth.base.BaseQueryModel;
import com.glory.erp.auth.emp.bean.Emp;
import com.glory.erp.auth.emp.bean.EmpQuery;
import com.glory.erp.auth.emp.dao.EmpDao;

/**
 * @Description: 员工Dao实现
 * @Title: EmpDaoImpl.java
 * @Author: 樊双峰
 * @Date: 2018年5月18日 上午12:52:51
 * @Version V1.0
 */
public class EmpDaoImpl extends BaseDaoImpl<Emp> implements EmpDao {

	public Emp getEmpByNameAndPassword(String userName, String password) {
		String hql = "from Emp where userName = ? and password = ?";
		List<Emp> list = this.getHibernateTemplate().find(hql, userName,
				password);
		return list.size() > 0 ? list.get(0) : null;
	}

	public void doQbc(DetachedCriteria dc, BaseQueryModel qm) {
		EmpQuery eqm = (EmpQuery)qm;
		if(eqm.getUserName()!=null && eqm.getUserName().trim().length()>0){
			dc.add(Restrictions.eq("userName", eqm.getUserName().trim()));
		}
		if(eqm.getName()!=null && eqm.getName().trim().length()>0){
			dc.add(Restrictions.like("name", "%"+eqm.getName().trim()+"%"));
		}
		if(eqm.getTele()!=null && eqm.getTele().trim().length()>0){
			dc.add(Restrictions.like("tele", "%"+eqm.getTele().trim()+"%"));
		}
		if(eqm.getGender()!=null && eqm.getGender()!= -1){
			dc.add(Restrictions.eq("gender", eqm.getGender()));
		}
		if(eqm.getEmail()!=null && eqm.getEmail().trim().length()>0){
			dc.add(Restrictions.like("email", "%"+eqm.getEmail().trim()+"%"));
		}
		//eqm.dm.uuid
		if(eqm.getDept()!=null && eqm.getDept().getUuid()!=null && eqm.getDept().getUuid()!=-1){
			//如果查询的条件是封装对象的uuid属性，可以使用两种格式构造
			//1.直接使用对象做查询条件:默认使用该对象的uuid作为查询条件(推荐)
			dc.add(Restrictions.eq("dept", eqm.getDept()));
			//2.使用对象的uuiid作为查询条件:仅支持使用uuid作为查询条件的格式
			//dc.add(Restrictions.eq("dm.uuid",eqm.getDm().getUuid()));
		}
		//birthday
		if(eqm.getBirthday()!=null){
			dc.add(Restrictions.ge("birthday", eqm.getBirthday()));
		}
		//birthday2
		if(eqm.getBirthday2()!=null){
			// 86400000 = 24*60*60*1000 一天的毫秒数
			dc.add(Restrictions.le("birthday", eqm.getBirthday2()+86400000-1));
		}
	}

	public boolean updatePwd(String userName, String password,
			String newPassword1, String newPassword2) {
		String hql = "update Emp set password = ? where userName = ? and password = ? and ? = ?";
		int row = this.getHibernateTemplate().bulkUpdate(hql,newPassword1,userName,password,newPassword1,newPassword2);
		return row>0;
	}

	public List<Emp> findAllByDeptUuid(Long uuid) {
		String hql = "from Emp where dept.uuid = ?";
		return this.getHibernateTemplate().find(hql,uuid);
	}
}